<?php
/**
 * Created by PhpStorm.
 * User: lihaitao
 * Date: 2019/9/10
 * Time: 上午10:17
 */

namespace app\admin\controller;

use \app\index\controller\Base;
use think\db;

class Adver extends Base
{
    //广告列表
    public function index()
    {
        $adver = Db::name("adver")->find(1);
        return view('index/adver',compact("adver"));
    }
    
    //开启关闭广告
    public function chgAdver()
    {
        $sta = request()->post("sta");

        $res = Db::name("adver")->where("id",1)->update(['ad_status' => $sta]);

        if ($res) {
            return $this->_toSuccess(['msg' => '操作成功']);
        }else{
            return $this->_toError(['msg' => '操作失败']);
        }
    }

    //修改图片
    public function chgImg()
    {
        $index = request()->post("index");
        $file = request()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            //echo $_SERVER['HTTP_HOST'];
            $info = $file->move('./album');
            if($info){

                $path = 'http://'.$_SERVER['HTTP_HOST'].'/album/'.$info->getSaveName();
                if ($index == 1) {
                    $field = "ad_zhuye_img1";
                }elseif($index == 2){
                    $field = "ad_zhuye_img2";
                }elseif($index == 3){
                    $field = "ad_list_img1";
                }elseif($index == 4){
                    $field = "ad_list_img2";
                }elseif($index == 5){
                    $field = "ad_detail_img1";
                }elseif($index == 6){
                    $field = "ad_detail_img2";
                }elseif($index == 7){
                    $field = "ad_my_img1";
                }elseif($index == 8){
                    $field = "ad_my_img2";
                }elseif($index == 9){
                    $field = "ad_lb1";
                }elseif($index == 10){
                    $field = "ad_lb2";
                }

                Db::name("adver")->where("id",1)->update([$field => $path]);

                echo "<script>alert('修改成功!');window.location.href='/admin/adver/index'</script>";

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

    }


    public function chgUrl()
    {
        $data = request()->post("data");
        $url  = request()->post("url");

        if ($data == 1) {
            $field = "zhuye_url";
        }elseif ($data == 2 ){
            $field = "list_url";
        }elseif ($data == 3){
            $field = "detail_url";
        }elseif ($data == 4){
            $field = "my_url";
        }

        $res = Db::name("adver")->where("id",1)->update([$field => $url]);

        if($res){
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }
    }
}